home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Beziers y otros splines / CanonicalSplineManual / CanonicalSplineManual.cs next >
Encoding:
Text File  |  2002-05-08  |  2.0 KB  |  57 lines

  1. //----------------------------------------------------
  2. // CanonicalSplineManual.cs ⌐ 2001 by Charles Petzold
  3. //----------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class CanonicalSplineManual: CanonicalSpline
  9. {
  10.      public new static void Main()
  11.      {
  12.           Application.Run(new CanonicalSplineManual());
  13.      }
  14.      public CanonicalSplineManual()
  15.      {
  16.           Text = "Spline can≤nico dibujado \"manualmente\"";
  17.      }
  18.      protected override void OnPaint(PaintEventArgs pea)
  19.      {
  20.           base.OnPaint(pea);
  21.  
  22.           CanonicalSpline(pea.Graphics, Pens.Red, apt, fTension);
  23.      }
  24.      void CanonicalSpline(Graphics grfx, Pen pen, Point[] apt, float T)
  25.      {
  26.           CanonicalSegment(grfx, pen, apt[0], apt[0], apt[1], apt[2], T);
  27.           CanonicalSegment(grfx, pen, apt[0], apt[1], apt[2], apt[3], T);
  28.           CanonicalSegment(grfx, pen, apt[1], apt[2], apt[3], apt[3], T);
  29.      }
  30.      void CanonicalSegment(Graphics grfx, Pen pen, Point pt0, Point pt1, 
  31.                            Point pt2, Point pt3, float T)
  32.      {
  33.           Point[] apt = new Point[10];
  34.  
  35.           float SX1 = T * (pt2.X - pt0.X);
  36.           float SY1 = T * (pt2.Y - pt0.Y);
  37.           float SX2 = T * (pt3.X - pt1.X);
  38.           float SY2 = T * (pt3.Y - pt1.Y);
  39.           float AX = SX1 + SX2 + 2 * pt1.X - 2 * pt2.X;
  40.           float AY = SY1 + SY2 + 2 * pt1.Y - 2 * pt2.Y;
  41.           float BX = -2 * SX1 - SX2 - 3 * pt1.X + 3 * pt2.X;
  42.           float BY = -2 * SY1 - SY2 - 3 * pt1.Y + 3 * pt2.Y;
  43.           float CX = SX1;
  44.           float CY = SY1;
  45.           float DX = pt1.X;
  46.           float DY = pt1.Y;
  47.  
  48.           for (int i = 0; i < apt.Length; i++)
  49.           {
  50.                float t = (float)i / (apt.Length - 1);
  51.                apt[i].X = (int) (AX * t * t * t + BX * t * t + CX * t + DX);
  52.                apt[i].Y = (int) (AY * t * t * t + BY * t * t + CY * t + DY);
  53.           }
  54.           grfx.DrawLines(pen, apt);
  55.      }
  56. }
  57.